﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DLL;

namespace BLL
{
    public static class xs_BLL
    {
        //创建数据对象
        private static dbExamDataContext db = new dbExamDataContext();

        /// <summary>
        /// 获取学生数据源
        /// </summary>
        /// <param name="bjNum">学生ID</param>
        public static object GetXSSource(string bjNum)
        {
            object obj = new object();
            obj = from p in db.XS orderby p.xsId where p.bjNum == decimal.Parse(bjNum) select new { p.BJ.bjName, p.xsId, p.xsIdCard, p.xsName,xsSex= p.xsSex==true?"男":"女" };
            return obj;
        }

        /// <summary>
        /// 获取学生数据源
        /// </summary>
        /// <param name="xh">学生ID</param>
        public static object GetXSInfo(string xh)
        {
            object obj = new object();
            obj = from p in db.XS orderby p.xsId where p.xsId == xh select new {p.BJ.ZY.YX.yxName,p.BJ.ZY.zyName,p.BJ.bjName,p.xsId,p.xsName };
            return obj;
        }
        /// <summary>
        /// 添加新学生
        /// </summary>
        /// <param name="newXS">新学生数据对象</param>
        public static bool AddNewXS(XS newXS)
        {
            bool bl = false;
            db = new dbExamDataContext();
            try
            {
                if (db.XS.Count(p =>p.xsId==newXS.xsId) > 0)
                {
                    return false;
                }
            }
            catch { }
            db.XS.InsertOnSubmit(newXS);
            db.SubmitChanges();
            bl = true;
            return bl;
        }
        /// <summary>
        /// 删除学生
        /// </summary>
        /// <param name="xsId">要删除的学生的编号</param>
        public static bool DeleteXS(string xsId)
        {
            bool bl = false;
            db = new dbExamDataContext();
            try
            {
                XS xs = db.XS.Single(p => p.xsId == xsId);
                db.XS.DeleteOnSubmit(xs);
                db.SubmitChanges();
                bl = true;
            }
            catch { }
            return bl;
        }

        /// <summary>
        /// 更新学生
        /// </summary>
        /// <param name="xsId">要更新的学生编号</param>
        /// <param name="newXS">新的数据内容</param>
        public static bool UpdateXS(string xsId, XS newXS)
        {
            bool bl = false;
            try
            {
                XS xs = db.XS.Single(p => p.xsId == xsId);
                xs.xsId = newXS.xsId;
                xs.xsIdCard = newXS.xsIdCard;
                xs.xsName = newXS.xsName;
                xs.xsSex = newXS.xsSex;
                xs.bjNum = newXS.bjNum;
                db.SubmitChanges();
                bl = true;
            }
            catch { }
            return bl;
        }
    }
}
